package com.guofen.controller.hsmpy;

import com.guofen.pojo.HsmSmallWorm;
import com.guofen.service.hsmpy.HatchedMaterialService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/hsmpy/HatchedMaterial")
@Controller
@Api(tags = "HatchedMaterialController", description = "3-1 孵化出料记录表")
public class HatchedMaterialController {

    @Autowired
    private HatchedMaterialService hatchedMaterialService;

    /*代码修改：增加是否培育(使用temp3)以及备注（使用temp1）*/
    @ApiOperation("增加记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name = "id", value = "id", required = true) @RequestParam String id,
                      @ApiParam(name = "hatch_end_time", value = "结束孵化日期", required = true) @RequestParam String hatch_end_time,
                      @ApiParam(name = "fhhlkfhw_weight", value = "孵化后卵壳+孵化网重", required = true) @RequestParam String fhhlkfhw_weight,
                      @ApiParam(name = "is_stop", value = "是否终止培育", required = true) @RequestParam String is_stop,
                      @ApiParam(name = "remark", value = "备注", required = false) @RequestParam String remark){
        Integer Id = Integer.parseInt(id);
        Double fhhlkfhw_w = Double.parseDouble(fhhlkfhw_weight);
        if (hatchedMaterialService.add(Id, hatch_end_time, fhhlkfhw_w, is_stop, remark))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("返回当天最新一条数据")
    @RequestMapping(value = "/getlatest", method = RequestMethod.GET)
    @ResponseBody
    public Result getlatest(){
        return ResultGenerator.genSuccessResult(hatchedMaterialService.getlatest());
    }

    @ApiOperation("返回还未填写过3-1内容的n条数据")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist(){
        return ResultGenerator.genSuccessResult(hatchedMaterialService.getlist());
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(hatchedMaterialService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="batch",value="批次",required=true) @RequestParam String batch,
                         @ApiParam(name="wormGroup",value="组别",required=true) @RequestParam String wormGroup,
                         @ApiParam(name="hatchBoxNumber",value="孵化盒号",required=true) @RequestParam String hatchBoxNumber,
                         @ApiParam(name="hatchEndTime",value="结束孵化日期",required=true) @RequestParam String hatchEndTime,
                         @ApiParam(name="fhhlkfhwWeight",value="孵化后壳网重（g）",required=true) @RequestParam Double fhhlkfhwWeight) {
        HsmSmallWorm record = new HsmSmallWorm();
        record.setId(id);
        record.setBatch(batch);
        record.setWormGroup(wormGroup);
        record.setHatchBoxNumber(hatchBoxNumber);
        record.setHatchEndTime(hatchEndTime);
        record.setFhhlkfhwWeight(fhhlkfhwWeight);
        return ResultGenerator.genSuccessResult(hatchedMaterialService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(hatchedMaterialService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(hatchedMaterialService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
